package org.xiyu.yee.beacon_anti_phantom.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.xiyu.yee.beacon_anti_phantom.Constants;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class ConfigManager {
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static final String CONFIG_FILE = "config/beacon_anti_phantom.json";
    
    public static class ConfigData {
        public Map<String, Integer> beaconRadius = new HashMap<>();
        
        public ConfigData() {
            // 默认配置
            beaconRadius.put("level_1", 20);
            beaconRadius.put("level_2", 30);
            beaconRadius.put("level_3", 40);
            beaconRadius.put("level_4", 50);
        }
    }
    
    public static void loadConfig() {
        try {
            Path configPath = Paths.get(CONFIG_FILE);
            
            if (Files.exists(configPath)) {
                // 加载现有配置
                try (Reader reader = Files.newBufferedReader(configPath)) {
                    ConfigData config = GSON.fromJson(reader, ConfigData.class);
                    if (config != null && config.beaconRadius != null) {
                        BeaconConfig instance = BeaconConfig.getInstance();
                        config.beaconRadius.forEach((key, value) -> {
                            try {
                                int level = Integer.parseInt(key.replace("level_", ""));
                                instance.setRadius(level, value);
                            } catch (NumberFormatException e) {
                                Constants.LOG.warn("Invalid level key in config: {}", key);
                            }
                        });
                        Constants.LOG.info("Configuration loaded from {}", CONFIG_FILE);
                    }
                }
            } else {
                // 创建默认配置文件
                saveConfig();
                Constants.LOG.info("Created default configuration file at {}", CONFIG_FILE);
            }
        } catch (Exception e) {
            Constants.LOG.error("Failed to load configuration", e);
        }
    }
    
    public static void saveConfig() {
        try {
            Path configPath = Paths.get(CONFIG_FILE);
            Files.createDirectories(configPath.getParent());
            
            ConfigData config = new ConfigData();
            BeaconConfig instance = BeaconConfig.getInstance();
            
            // 保存当前配置
            instance.getAllRadius().forEach((level, radius) -> 
                config.beaconRadius.put("level_" + level, radius));
            
            try (Writer writer = Files.newBufferedWriter(configPath)) {
                GSON.toJson(config, writer);
                Constants.LOG.info("Configuration saved to {}", CONFIG_FILE);
            }
        } catch (Exception e) {
            Constants.LOG.error("Failed to save configuration", e);
        }
    }
}
